﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using EntityFramework.Extensions;
namespace Webcar.Controllers
{
    public partial class ReportController : BaseControler
    {
        public ActionResult List()
        {
            return View();
        }
        public ActionResult ListPartialView(string id)
        {
            return PartialView();
        }
        public ActionResult Delete(string id)
        {
            CurrentUser.EnsurePermission(PermissionManager.填写情况汇报);
            using (var DB = new WebcarContext())
            {
                var model = DB.SEC_MONTH_REPORT.Find(id);
                if (model != null)
                {
                    DB.SEC_MONTH_REPORT.Remove(model);
                    DB.SaveChanges();
                }
                return Success("List", "Report", new { id = model.DEPT_CODE });
            }
            
        }
        [HttpGet]
        public ActionResult Edit(string id, string ReturnUrl)
        {
            SEC_MONTH_REPORT model = null;
            if (id.IsNotNull())
            {
                model = WebcarDataProvider.DB.SEC_MONTH_REPORT.Find(id);
            }
            if (model == null)
            {
                model = new SEC_MONTH_REPORT();
                model.man_name = CurrentUser.GetLoginUserName();
            }
            return View(model: model);
        }
        [HttpPost]
        public ActionResult Edit(SEC_MONTH_REPORT model,string ReturnUrl)
        {
            CurrentUser.EnsurePermission(PermissionManager.填写情况汇报);
            string error = "";
            if (CheckCarModel(model, ref error) == false)
            {
                ViewBag.ErrorMessage = error;
                return View(model: model);
            }


            SEC_MONTH_REPORT item = null;
            using (var DB = new WebcarContext())
            {
                if (model.XH.IsNotNull())
                {
                    item = DB.SEC_MONTH_REPORT.Find(model.XH);
                }
                else
                {
                    item = new SEC_MONTH_REPORT();
                    DB.SEC_MONTH_REPORT.Add(item);
                }

                model.CopyPropertyTo(item);
                if (item.XH.IsNull())
                    item.XH = "M" + DB.GetSearialId();
                DB.SaveChanges();
            }
            if (ReturnUrl.IsNotNull())
            {
                return new RedirectResult(ReturnUrl);
            }
            else
            {
                return RedirectToAction("List", "Report", new { id = item.DEPT_CODE });
            }

            //return Success("List", "Report", new { id = item.DEPT_CODE });
        }

        bool CheckCarModel(SEC_MONTH_REPORT model, ref string error)
        {
            if (model.NY.IsNull())
            {
                error = "请输入月份。";
                return false;
            }
            if ((model.NY + "-01").IsDate() == false)
            {
                error = "月份格式错误，示例：2010-09，请检查。";
                return false;
            }
            if (model.DEPT_CODE.IsNull())
            {
                error = "请选择部门。";
                return false;
            }
            if (model.REPORT_KIND==null)
            {
                error = "请选择出车时间。";
                return false;
            }
            if (model.CONTENT.IsNull())
            {
                error = "请输入申请人。";
                return false;
            }
            return true;
        }



        [HttpGet]
        public ActionResult ResponseView(string id,string ReturnUrl)
        {
            var model = WebcarDataProvider.DB.report_view_response.Find("report", id.ToString(), CurrentUser.LoginUserID);
            if (model == null)
            {
                model = new report_view_response();
                model.response_note = "已阅";
                model.table_id = id;
            }
            return View("ResponseView", model);
        }
        [HttpPost]
        public ActionResult ResponseView(report_view_response model, string ReturnUrl)
        {
            using (var DB = new WebcarContext())
            {
                var exists = DB.report_view_response.Find("report", model.table_id, CurrentUser.LoginUserID);
                if (exists == null)
                {
                    var item = new report_view_response();
                    item.table_id = model.table_id;
                    item.table_name = "report";
                    item.opercode = CurrentUser.LoginUserID;
                    item.response_note = model.response_note;
                    item.response_date = DateTime.Now;
                    DB.report_view_response.Add(item);
                }
                else
                {
                    exists.response_note = model.response_note;
                }
                DB.SaveChanges();
            }
            if (ReturnUrl.IsNotNull())
            {
                return new RedirectResult(ReturnUrl);
            }
            else
            {
                return RedirectToAction("List");
            }
        }

        public ActionResult ResponseListPartialView(string id)
        {
            var list = from p in WebcarDataProvider.DB.report_view_response
                       join p2 in WebcarDataProvider.DB.v_all_users
                       on p.opercode equals p2.account
                       into result
                       from bb in result.DefaultIfEmpty()
                       where p.table_name == "report" && p.table_id == id
                       select new
                       {
                           user_name = bb.name,
                           response_date = p.response_date,
                           response_note = p.response_note
                       };

            return PartialView(list.ToList());
        }
    }
}